package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.actuator.GenericActuator;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

public class SubstringActuator implements GenericActuator {
    @Override
    public Object execute(Object source, Object... args) {
        String str = null == source ? "" : String.valueOf(source);
        Object startIndexObj = args[0];
        Object endIndexObj = args.length > 1 ? args[1] : null;
        Integer startIndex;
        try {
            startIndex = ObjectTypeUtils.convertType(startIndexObj, Integer.class);
        } catch (Exception e) {
            return source;
        }
        Integer endIndex = null;
        if (endIndexObj instanceof Integer) {
            endIndex = ObjectTypeUtils.convertType(endIndexObj, Integer.class);
        }
        return null == endIndex ? str.substring(startIndex) : str.substring(startIndex, endIndex);
    }
}
